#include "Enemy.h"


Enemy::Enemy(void) : mActive(false)
{
	 mTimer = 0.0f;
}

Enemy::~Enemy(void)
{
}

void Enemy::Load()
{
	mSprite.Load("pikachu.png");
}

void Enemy::Unload()
{
	mSprite.Unload();
}

void Enemy::Update(float deltaTime)
{
	if(mActive)
	{
		mTimer += deltaTime * 5.0f;
		mTrackPosition += mVelocity * deltaTime;
		mPosition.x = mTrackPosition.x + (50.0f *sin(mTimer));
		mPosition.y = mTrackPosition.y + (50.0f *cos(mTimer));

		//mDelay -= deltaTime;
		//if(mDelay <= 0.0f)
		//{
		//	SVector2 mtemp = mVelocity; 
		//	mtemp.x = -mVelocity.x;
		//	mVelocity = mtemp;
		//	mVelocity.x *= -1.0f;
		//	mDelay = RandomFloat(0.5f, 2.0f);
		//}

		const int kWinWidth = IniFile_GetInt("WinWidth", 800);
		const int kWinHeight = IniFile_GetInt("WinHeight", 600);
		const int kThreshold = mSprite.GetWidth();
		if(mPosition.x < -kThreshold || mPosition.x > kWinWidth + kThreshold || 
			mPosition.y < -kThreshold || mPosition.y > kWinHeight + kThreshold)
		{
			Kill();
		}
	}
}


void Enemy::Render()
{
	if(mActive)
	{
		const int width = mSprite.GetWidth();
		const int height = mSprite.GetHeight();
		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset;
		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
}

void Enemy::Spawn(const SVector2& pos)
{
	mPosition = pos;
	mVelocity = SVector2(200.0f, 200.0f);
	mActive = true;
}

void Enemy::Kill()
{
	mActive = false;
}